'use strict';

const { Controller, Utils, Storage, Service } = require('ee-core');
const { exec } = require('child_process');
const { killProcess } = require('../utils');
const path = require('path');
let task;

class LocalServeController extends Controller {
  constructor(ctx) {
    super(ctx);
  }

  async openLocalServe() {
    this.app.service.localService.start(); //本地服务
    this.app.service.localServiceStorage.start(); //本地服务 sqlite
    return '启动成功';
  }

  closeLocalServe() {
    return this.app.service.localService.end();
  }

  addTaskData(data) {
    return this.app.service.localServiceStorage.addTaskDataSqlite(data);
  }

  updateTaskDataById(params) {
    return this.app.service.localServiceStorage.updateTaskDataSqlite(params.id, params.progress);
  }

  getAllTaskData() {
    return this.app.service.localServiceStorage.getAllTaskDataSqlite();
  }

  getSingleTaskData(id) {
    return this.app.service.localServiceStorage.getSingleTaskData(id);
  }

  delTaskDataSqliteById(id) {
    return this.app.service.localServiceStorage.delTaskDataSqlite(id);
  }

  /**
   * 启动websocket
   * **/
  startWebsocket() {
    return this.app.service.websocket.start();
  }

  /**
   * @desc 关闭socket服务连接
   */
  endWebsocket() {
    return this.app.service.websocket.end();
  }

  /**
   * @desc 方便client 主动调用发送,一般很少调用
   */
  wsConnSendText(text) {
    this.app.service.websocket.wsConnSendText(text);
    return '发送成功';
  }
}

LocalServeController.toString = () => ['class LocalServeController'];
module.exports = LocalServeController;
